package com.shenmazong.thread;

/**
 * @program: server-java-demo
 * @description: ThreadStopDemo
 * @author: 亮子说编程
 * @create: 2020-11-11 16:03
 **/

class StopThread implements Runnable {

    @Override
    public void run() {
        while(true) {
            System.out.println("i am working");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            if(ThreadStopDemo.stop) {
                System.out.println("再见了");
                break;
            }
        }
    }
}

public class ThreadStopDemo {

    static volatile boolean stop = false;

    public static void main(String[] args) {
        StopThread stopThread = new StopThread();

        new Thread(stopThread).start();

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //stopThread.setStop(true);

        //stop = true;
    }
}
